Online-Academy
Look, Read, Understand, Apply

Operating System

CPU Scheduling

CPU Scheduling

CPU scheduling is the process by which an operating system decides which process (or thread) gets to use the CPU next and for how long. Since many programs may be ready to run at the same time, the CPU scheduler ensures they share the processor efficiently.

A computer often has multiple programs running simultaneously. Because a CPU can execute only one process at a time (per core), the operating system must decide:

  • Which process runs next.
  • How long it runs.
  • When to switch to another process.
The goals of CPU scheduling are to:
  • Maximize CPU utilization.
  • Increase system throughput (more work completed).
  • Minimize waiting time.
  • Minimize turnaround time.
  • Reduce response time for interactive users.
  • Ensure fairness among processes.

How CPU scheduling works?

  • Processes enter the ready queue.
  • The CPU scheduler selects one process from the queue.
  • The selected process executes on the CPU.
  • When it finishes, blocks for I/O, or its time slice expires, the scheduler chooses another process.

Types of CPU Scheduling

Non-Preemptive Scheduling

Once a process gets the CPU, it continues until it finishes or blocks for I/O. Example: First Come, First Served (FCFS).

Advantages:
  • Simple to implement.
  • Low overhead.
Disadvantages:
  • Long processes can delay shorter ones.

Preemptive Scheduling

The operating system can interrupt a running process and assign the CPU to another process. Example: Round Robin (RR).

Advantages:
  • Better response time.
  • More suitable for interactive systems.
Disadvantages:
  • More context switching overhead.

FCFS Scheduling

ProcessArrival TimeBurst Time
P1 0 8
P2 1 4
P3 2 2
P4 3 5
P5 4 3

Since all processes arrive in the order P1-> P2->P3 -> P4 -> P5, FCFS executes them in that order:

Gantt chart

0        8       12      14       19      22
|   P1   |   P2   |  P3  |   P4   |  P5   |    
  • Turnaround Time (TAT) = CT - AT
  • Waiting Time (WT) = TAT - BT
ProcessATBTCT TATWT
P1 08880
P21412117
P322141210
P435191611
P543221815
8.613

Non-Preemptive SJF

ProcessArrival TimeBurst Time
P107
P224
P341
P454

At time 0, only P1 has arrived, so P1 executes first. At time 7, P2, P3, and P4 are available. P3 has the shortest burst time, so P3 executes ntext. The P2 and P4 both have 4ms. We choose P2 first because it arrived earlier.

0        7    8        12       16
|   P1   | P3 |   P2   |   P4   |
0
ProcessATBTCTTAT = CT-ATWT = TAT-BT
P17770
P141843
P32412106
P45416117
8ms4ms

Round Robin Scheduling

ProcessBurst time
P110
P25
P38
P46

All arrive at time 0. Time Quantum = 3ms

0    3    6    9    12   15   18   21   23   26   29
| P1 | P2 | P3 | P4 | P1 | P2 | P3 | P4 | P1 | P3 |

Let's calculate carefully:

  • P1: 10 -> remaining 7
  • P2: 5 -> remaiing 2
  • P3: 8 -> remaiing 5
  • P4: 6 -> remaining 3
  • P1: 7 -> remaining 4
  • P2: 2 -> finishes at 17
  • P3: 5 -> remaining 2
  • P4: 3 -> finishes at 23
  • P1: 4 -> remaining 1
  • P3: 2 -> finishes at 28
  • P1: 1 -> finishes at 29
Gantt Chart
0    3    6    9    12   15   17   20   23   26   28   29
| P1 | P2 | P3 | P4 | P1 | P2 | P3 | P4 | P1 | P3 | P1 |
Completion Time
ProcessBTCTTATWT
P110292919
P25171712
P38282820
P4623 2317
24.25ms17ms

Priority Scheduling

ProcessBurst TimePriority
P1103
P211
P324
P412
Smaller number = higher priority

Therefore execution order is:
P2 -> P4 -> P1-> P3

Gantt Chart
0    1    2          12    14
| P2 | P4 |    P1    | P3 |

All processes arrive at time 0.

ProcessBTCTTATWT
P21110
P41221
P11012122
P32141412
7.25ms3.75ms

TAT = CT - AT

WT = TAT = BT

Average WT = sumation(WT)/Number of Processes

Average TAT = sumation(TAT)/Number of Processes